﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;

namespace Platformer1
{
    class Powerup:Gem
    {
        private Texture2D texture;
        private Vector2 origin;
        private SoundEffect collectedSound;
        
        private byte red=255, green=0, blue=0;
        private Color color; 

        private Level level;

        public Powerup(Level level, Vector2 position):base(level, position)
        {
            this.level = level;
            this.basePosition = position;

            this.LoadContent();
        }

        public new void LoadContent()
        {
            texture = Level.Content.Load<Texture2D>("Sprites/unicorn");
            origin = new Vector2(texture.Width / 2.0f, texture.Height / 2.0f);
            collectedSound = Level.Content.Load<SoundEffect>("Sounds/GemCollected");
            color = new Color(red, green, blue);
        }

        public new void Update(GameTime gameTime)
        {
            // Bounce control constants
            const float BounceHeight = 0.5f;
            const float BounceRate = 3.0f;
            const float BounceSync = -0.75f;

            // Bounce along a sine curve over time.
            // Include the X coordinate so that neighboring gems bounce in a nice wave pattern.            
            double t = gameTime.TotalGameTime.TotalSeconds * BounceRate + Position.X * BounceSync;
            bounce = (float)Math.Sin(t) * BounceHeight * texture.Height;

            setTransColor();
            setTransColor();
            setTransColor();
            setTransColor();
        }

        public new void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, Position, null, color, 0.0f, origin, 1.0f, SpriteEffects.None, 0.0f);
        }

        public void setTransColor()
        {
            if ((red == 255)&&(green < 255)&&(green >= 0)&&(blue == 0)) { green++; }
            else if ((red <= 255)&&(red > 0) && (green == 255) && (blue == 0)) { red--; }
            else if ((red == 0) && (green == 255) && (blue < 255) && (blue >= 0)) { blue++; }
            else if ((red == 0) && (green <= 255) && (green > 0) && (blue == 255)) { green--; }
            else if ((red < 255) && (red >= 0) && (green == 0) && (blue == 255)) { red++; }
            else if ((red == 255) && (green == 0) && (blue > 0) && (blue <= 255)) { blue--; }
            color = new Color(red, green, blue);
        }

        override public void OnCollected(Player collectedBy)
        {
            collectedSound.Play();
            collectedBy.spawnUnicorn();
        }
    }
}
